package com.boe.common.model;

import lombok.Data;
import lombok.SneakyThrows;
import lombok.var;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.CompressionMethod;

import java.io.File;
import java.io.InputStream;
import java.util.Arrays;

@Data
public class ZipCompressData {
    private ZipFile zipFile;
    private ZipParameters zipParameters = new ZipParameters();

    public ZipCompressData(File zipFile) {
        this.zipFile = new ZipFile(zipFile);

        this.zipParameters = getNewZipParameter();
    }

    private ZipParameters getNewZipParameter() {
        var zipParameters = new ZipParameters();
        zipParameters.setCompressionMethod(CompressionMethod.DEFLATE);
        zipParameters.setCompressionLevel(CompressionLevel.NORMAL); // 压缩级别
        zipParameters.setOverrideExistingFilesInZip(true);
        return zipParameters;
    }


    @SneakyThrows
    public ZipCompressData add(File file) {
        if (file.isFile()) {
            zipFile.addFile(file, zipParameters);
            return this;
        }

        zipFile.addFolder(file, zipParameters);
        return this;
    }

    @SneakyThrows
    public ZipCompressData add(InputStream stream, String fileName) {

        var zipParameters = getNewZipParameter();
        zipParameters.setFileNameInZip(fileName);

        zipFile.addStream(stream, zipParameters);
        return this;
    }
}
